<?php
/*
	分销中心
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
//$this->model('member')->set_parent(90,1);
model('jy_commission/member')->min_agent_level();
model('jy_commission/member')->upyao_fee();
model('jy_commission/member')->upbao_agent();
plugin('jy_commission')->session('footernav',$return['pluginset']['footernav']);
plugin('jy_commission')->session('pluginname','jy_bufee');
if($_RQ['op']=='main'){
	$return['setting']=plugin('jy_commission')->plugin_setting('page_setting');
	set_html('title',$return['setting']['title']?:'分销中心');
	$basic=model('core/member')->basic($_SESSION['uid']);
	$member=array_merge($member,$basic);
	if($member['agent_id']){
		$member['agent']=model('core/member')->basic($member['agent_id']);
	}else{
		$member['agent']=array('nickname'=>'总店');
	}
	if($member['level_id']){
		$member['level']=pdo_getcolumn('jy_commission_level',array('id'=>$member['level_id']),'title');
	}else{
		$member['level']=$return['setting']['demolevel'];
	}
	$return['ordernum']=0;
	$cengkey=1;
	$agent_id=$member['agent_id'];
	for($i=0;$i<$this->setting['level'];$i++){
		$level=$i+1;
		$return['ordernum'] +=pdo_count('jy_commission_commission',array('mid'=>$_SESSION['uid'],'level'=>$level));
		if($agent_id){
			$agent_id=pdo_getcolumn('jy_commission_member',array('mid'=>$agent_id),'agent_id');
			$cengkey++;
		}
	}
	if($member['t_time']  >0){
    	$member['b_time'] = date('Y-m-d',$member['t_time']);
	}else{
	    $member['b_time'] = 0;
	}
	$member['cengkey']=$cengkey;
	$member['cengtitle']=$this->setting['levelname'.$cengkey]?:$cengkey.'级分销';
	$return['member']=$member;
	pdo_update('jy_commission_commission',array('isxuni'=>1),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'level'=>array(18,19),'isxuni'=>0));
	$data=array();
	//已结算佣金
	$data['balance_total']=pdo_getsum('jy_commission_balance',array('mid'=>$_SESSION['uid']),'money');
	$data['balance_num']=pdo_count('jy_commission_balance',array('mid'=>$_SESSION['uid']));
	//待结算佣金
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'isxuni'=>0);
	/*if($this->setting['balance_day']){
		//结算天数
		$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
		$params['complete_time <']=$time;
	}*/
	$data['commission_1']=pdo_getsum('jy_commission_commission',$params,'money');
	$data['commission_total']=pdo_getsum('jy_commission_commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'money');
	$data['commission_area']=pdo_getsum('jy_commission_commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'level'=>16),'money');
	$data['commission_xuni']=pdo_getsum('jy_commission_commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'level'=>array(18,19)),'money');
	$levels=array();
	$agent_ids=$_SESSION['uid'];
	$allagentnum=0;
	$allmember_num=0;
	for($i=0;$i<18;$i++){
		$level=$i+1;
		if(!$agent_ids){
			$levels[$level]=0;
			continue;
		}
		$agent_ids=pdo_getall('jy_commission_member',array('agent_id'=>$agent_ids),array('mid'),'mid');
		if(!$agent_ids){
			$levels[$level]=0;
		}else{
			$agent_ids=array_keys($agent_ids);
			if($i<$this->setting['level']){
				$levels[$level]=count($agent_ids);
			}
			$allmember_num+=$levels[$level];
			$member_ids=pdo_getall('jy_commission_member',array('agent_id'=>$agent_ids,'is_agent'=>1),array('mid'),'mid');
			$allagentnum+=count($member_ids);
		}
	}
	$return['game_wait']=model('jy_weishop/task')->pop_game('',8);
	$agent_update=array('agent_num'=>$allagentnum,'member_num'=>$allmember_num);
	pdo_update('jy_commission_member',$agent_update,array('mid'=>$_SESSION['uid']));
	$return['member']['member_num']=$allmember_num;
	$return['member']['saleurl']=custom_url('jy_commission/myshop//sale_mid='.$_SESSION['uid'],true);
	if($_SESSION['fromh5wxapp'] || $_SESSION['container']=='wxapp'){
			$image='oss/'.$_SESSION['uniacid'].'/images/jy_poster/wxappid_jy_bufee'.$_SESSION['uid'].'.jpg';
			$isadd=cfc('files')->source_check($image);
			if(!$isadd && $_SESSION['wxapp_id']){
				$url='jy_commission/myshop//sale_mid='.$_SESSION['uid'];
				$url='/pages/index/index?url='.urlencode($url);
				$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
				$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
				$path='oss/'.$_SESSION['uniacid'].'/images/jy_poster/';
				if(!file_exists(ROOT_AT.$path)){
					mkdir(ROOT_AT.$path,0700, true);
				}
				$filename=$path.'/wxappid_jy_bufee'.$_SESSION['uid'].'.jpg';
				$return['qrcode']=cfc('files')->source_save($res,'nochoose','png','二维码',$filename,'',$url);
			}
	}else{
		$return['qrcode']=cfc('tools')->createqrcode($return['member']['saleurl'],'250',true);
	}
	$return['qrcode']=tomedia($return['qrcode']);
	$jy_poster = $this->plugin_exists('jy_poster');
	if($jy_poster){
		$poster=$this->plugin_setting('poster');
		if($poster['isopen']){
			$return['qrcodeurl']='jy_poster/apibuild//plugin_name=jy_bufee';
		}
	}
	$return['data']=$data;
	$return['xuniname']=$this->xuniname;
	exi($return);
}
if($_RQ['op']=='apply'){
	$apply=pdo_get('jy_commission_apply',array('mid'=>$_SESSION['uid']));
	if($apply['status']==0 && $apply['id']){
		exi('您已申请，请等待审核！','error');
	}
		$post=get_postdata();
		if(!$post['agree']){
			exi('请先阅读协议！','error');
		}
		if(!$post['realname']){
			exi('请输入姓名！','error');
		}
		if(!$post['mobile']){
			exi('请输入联系电话！','error');
		}
		if($post['diyformfields']){
			foreach ($post['diyformfields'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
		}
		$member_update=array(
			'realname'=>trim($post['realname']),
			'mobile'=>trim($post['mobile']),
			'diyformdata'=>ijson_encode($post['diyformfields']),
		);
		if($post['wechat']){
			$member_update['wechat']=trim($post['wechat']);
		}
		model('jy_commission/member')->member_update($_SESSION['uid'],$member_update);	
		if($post['agent_id']){
			$agent_id=intval($post['agent_id']);
			if($agent_id){
				model('jy_commission/member')->set_parent($_SESSION['uid'],$agent_id);
			}
		}
		model('jy_commission/member')->apply_agent($_SESSION['uid']);
		exi('申请成功！请耐心等待管理员审核！','',custom_url('jy_weishop/index/',true));
}
?>